package com.bawei.shopping.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class YaoBezierView extends RelativeLayout {

    public YaoBezierView(Context context) {
        super(context);
    }

    public YaoBezierView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YaoBezierView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public YaoBezierView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    private List<Integer> mList = new ArrayList<>();//图片集合



    /**
     * 添加图片
     * @param images
     */
    public void addImages(int... images){
        for (int image : images) {
            mList.add(image);
        }
    }

    /**
     * 点赞：
     */
    public void addFavor(){
        //0：创建ImageView
        ImageView imageView = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(50, 50);
        imageView.setLayoutParams(layoutParams);
        int index = (int)(Math.random()*mList.size());//随机取一张图片
        imageView.setImageResource(mList.get(index));
        addView(imageView);
        //1：确定出四个点的坐标
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        PointF startPointF = new PointF(width/2,height);
        PointF  endPointF = new PointF(width/2,0);
        PointF pointF1 = new PointF((int)(Math.random()*width),height*2/3);
        PointF pointF2 = new PointF((int)(Math.random()*width),height/3);
        //2:动画
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new MyEvaluator(pointF1, pointF2), startPointF, endPointF);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();//0-1
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                //设置透明度
                imageView.setAlpha( 1 -fraction );//逐渐消失

            }
        });
        valueAnimator.start();
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(imageView);
            }
        });
    }


    //估值器
    class MyEvaluator implements TypeEvaluator<PointF> {
        private PointF mPointF1;//途经点1
        private PointF mPointF2;//途经点2

        public MyEvaluator(PointF pointF1, PointF pointF2) {
            mPointF1 = pointF1;
            mPointF2 = pointF2;
        }

        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {


            PointF pointF = new PointF();
            pointF.x = startValue.x*(1-fraction)*(1-fraction)*(1-fraction)
                    +3*mPointF1.x*fraction*(1-fraction)*(1-fraction)
                    +3*mPointF2.x*fraction*fraction*(1-fraction)
                    +endValue.x*fraction*fraction*fraction;

            pointF.y = startValue.y*(1-fraction)*(1-fraction)*(1-fraction)
                    +3*mPointF1.y*fraction*(1-fraction)*(1-fraction)
                    +3*mPointF2.y*fraction*fraction*(1-fraction)
                    +endValue.y*fraction*fraction*fraction;

            return pointF;
        }
    }


}
